//
//  AddFriendViewController.m
//  BaChat
//
//  Created by 刘洋 on 16/6/14.
//  Copyright © 2016年 刘洋. All rights reserved.
//

#import "AddFriendViewController.h"
#import "SweepViewController.h"
@interface AddFriendViewController ()<UITextFieldDelegate>

@property(nonatomic,retain)UITextField *searchNameTextField;


@end

@implementation AddFriendViewController

-(void)dealloc{
    [_searchNameTextField release];
    
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
  
  self.navigationItem.title = @"添加好友";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"navigationbar_back_light"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction)];
  
    
    
    BaseView *bgView = [[BaseView alloc]initWithFrame:CGRectMake(0, 10, ROOTWIDTH, 40)];
    bgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bgView];
    [bgView release];
    
    
    
    BaseImageView *findImage = [[BaseImageView alloc]initWithImage:[UIImage imageNamed:@"add_friend_searchicon" ]];
    findImage.frame = CGRectMake(10, 10, 50, 40);
    findImage.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:findImage];
    [findImage release];
    
    
    self.searchNameTextField = [[UITextField alloc]initWithFrame:CGRectMake(60, 10, ROOTWIDTH, 40)];
    self.searchNameTextField.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.searchNameTextField];
    [_searchNameTextField release];
    self.searchNameTextField.delegate = self;
    
    
    
    
    BaseButton *addButton = [BaseButton buttonWithType:UIButtonTypeCustom];
    addButton.backgroundColor = [UIColor redColor];
    [self.view addSubview:addButton];
    [addButton setTitle:@"添加好友" forState:UIControlStateNormal];
    [addButton addTarget:self action:@selector(addFriendButton) forControlEvents:UIControlEventTouchUpInside];
    [addButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(70);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(40);
        addButton.layer.masksToBounds = YES;
        addButton.layer.cornerRadius = 5;
        
    }];
    
    
    BaseButton *sweepButton = [BaseButton buttonWithType:UIButtonTypeCustom];
    sweepButton.backgroundColor = [UIColor redColor];
    [self.view addSubview:sweepButton];
    [sweepButton setTitle:@"扫一扫添加" forState:UIControlStateNormal];
    [sweepButton addTarget:self action:@selector(sweepFriendButton) forControlEvents:UIControlEventTouchUpInside];
    [sweepButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(addButton.mas_bottom).offset(20);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(40);
        sweepButton.layer.masksToBounds = YES;
        sweepButton.layer.cornerRadius = 5;
        
    }];
    
    
    
    
    
    // Do any additional setup after loading the view.
}

-(void)leftAction{
    [self.navigationController popViewControllerAnimated:YES];
}


-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.searchNameTextField resignFirstResponder];
}


-(void)addFriendButton{
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeCustomView;
    [hud hide:YES afterDelay:1];
    
    
    NSString *name = self.searchNameTextField.text;
  
    if (name.length == 0) {
        hud.labelText = @"请输入搜索用户名";
    }else{
        
  
//向服务器发送好友请求
    EMError *error = nil;
    [[EaseMob sharedInstance].chatManager addBuddy:name message:@"我想加您为好友" error:&error];
    if (!error) {
        NSLog(@"发送好友请求成功");
        hud.labelText = @"发送成功";
        
        
        
        
    }else{
        NSLog(@"发送好友失败");
        hud.labelText = @"发送失败或找不到该用户";
        NSLog(@"%@",error);
    }
    
}
    
}


-(void)sweepFriendButton{
    

    

    SweepViewController *sweepVC = [[SweepViewController alloc]init];
    sweepVC.isAddFriend = YES;
    sweepVC.hidesBottomBarWhenPushed = YES;
    sweepVC.hudBlock = ^(NSString *hudString){
        
        
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeCustomView;
        [hud hide:YES afterDelay:1];
        hud.labelText = hudString;
        
    };
    
    
    [self.navigationController pushViewController:sweepVC animated:YES];

    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
